from ipmininet.iptopo import IPTopo
from ipmininet.router.config import RouterConfig, Openr, SSHd
from ipmininet.ipnet import IPNet
from ipmininet.cli import IPCLI
from ipmininet.link import TCIntf
from mininet.util import custom
from mininet.link import TCLink

HOSTS_PER_ROUTER = 2

class MyTopo(IPTopo):
    def build(self, *args, **kw):
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        h3 = self.addHost('h3')
        self.addLink(h1, h2)
        self.addLink(h2, h3)
        super().build(*args, **kw)

class OpenrConfig(RouterConfig):
    """A simple config with only a OpenR daemon"""
    def __init__(self, node, *args, **kwargs):
        defaults = {"enable_v4": True}
        super().__init__(node, daemons=((Openr, defaults),), *args, **kwargs)


class SimpleOpenrNet(IPTopo):
    """
        +----+     +----+
        | r1 +-----+ r2 |
        +--+-+     +----+
           |
           |       +----+
           +-------+ r3 |
                   +----+
    """

    def build(self, *args, **kwargs):
        r1, r2, r3 = self.addRouters('r1', 'r2', 'r3', config=OpenrConfig, privateDirs=['/tmp', '/var/log'])
        self.addLink(r1, r2)
        link = self.addLink(r2, r3)
        self.addLink(r1, self.addHost('h0'))
        self.addLink(r3, self.addHost('h1'))
        super().build(*args, **kwargs)

    def build1(self, *args, **kwargs):
        r1, r2, r3, r4 = self.addRouters('r1', 'r2', 'r3', 'r4', config=OpenrConfig,
                                     privateDirs=['/tmp', '/var/log'])
        self.addLinks((r1, r2), (r2, r3), (r3, r4), (r4, r1))
        # self.addLinks((r1, r2), (r2, r3), (r3, r4))
        for r in (r1, r2, r3, r4):
            for i in range(HOSTS_PER_ROUTER):
                self.addLink(r, self.addHost('h%s%s' % (i, r)),
                             params2={'v4_width': 5})

        super().build(*args, **kwargs)


net = IPNet(topo=SimpleOpenrNet(), autoStaticArp=True)
try:
    net.start()
    IPCLI(net)
finally:
    net.stop()
